package com.hs.communication.common.encrypt;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * <h1>AES 加密和解密示例代码</h1>
 * Created by woniu
 */
public class AESEncrypt {


    public static byte[] aes_key; //密钥
    public static byte[] aes_iv; //16字节的初始向量,必须16字节

    /**
     * 加密模式之 ECB，算法/模式/补码方式
     */
    private static final String AES_ECB = "AES/ECB/PKCS5Padding";

    /**
     * 加密模式之 CBC，算法/模式/补码方式
     */
    private static final String AES_CBC = "AES/CBC/PKCS5Padding";

    /**
     * 加密模式之 CFB，算法/模式/补码方式
     */
    private static final String AES_CFB = "AES/CFB/PKCS5Padding";

    /**
     * AES 中的 IV 必须是 16 字节（128位）长
     */
    private static final Integer IV_LENGTH = 16;

    /***
     * <h2>空校验</h2>
     * @param arr 需要判断的值
     */
    private static boolean isEmpty(byte[] arr) {
        return null == arr || arr.length == 0;
    }

    private static boolean isEmpty(String str) {
        return null == str || str.equals("");
    }

    /***
     * <h2>String 转 byte</h2>
     * @param str 需要转换的字符串
     */
    private static byte[] getBytes(String str) {
        if (isEmpty(str)) {
            return null;
        }

        try {
            return str.getBytes(StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /***
     * <h2>初始化向量（IV），它是一个随机生成的字节数组，用于增加加密和解密的安全性</h2>
     */
    private static String getIV() {
        String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < IV_LENGTH; i++) {
            int number = random.nextInt(str.length());
            sb.append(str.charAt(number));
        }
        return sb.toString();
    }

    /***
     * <h2>获取一个 AES 密钥规范</h2>
     */
    private static SecretKeySpec getSecretKeySpec(byte[] key) {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
        return secretKeySpec;
    }

    /**
     * <h2>加密 - 模式 ECB</h2>
     *
     * @param msg 需要加密的文本内容
     * @param key 加密的密钥 key
     */
    private static byte[] encrypt(byte[] key, byte[] msg) {
        if (msg.length == 0 || isEmpty(key)) {
            return null;
        }

        try {
            // 创建AES加密器
            Cipher cipher = Cipher.getInstance(AES_ECB);

            SecretKeySpec secretKeySpec = getSecretKeySpec(key);

            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

            // 加密字节数组
            byte[] encryptedBytes = cipher.doFinal(msg);

            // 将密文转换为 Base64 编码字符串
//            return Base64.getEncoder().encodeToString(encryptedBytes);
            return encryptedBytes;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * <h2>解密 - 模式 ECB</h2>
     *
     * @param msg 需要解密的文本内容
     * @param key 解密的密钥 key
     */
    private static byte[] decrypt(byte[] key, byte[] msg) {
        if (isEmpty(msg) || isEmpty(key)) {
            return null;
        }

        // 将密文转换为16字节的字节数组
//        byte[] textBytes = Base64.getDecoder().decode(text);

        try {
            // 创建AES加密器
            Cipher cipher = Cipher.getInstance(AES_ECB);

            SecretKeySpec secretKeySpec = getSecretKeySpec(key);

            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);

            // 解密字节数组
            byte[] decryptedBytes = cipher.doFinal(msg);

            // 将明文转换为字符串
//            return new String(decryptedBytes, StandardCharsets.UTF_8);
            return decryptedBytes;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * <h2>加密 - AES_CFB自定义加密模式</h2>
     *
     * @param msg 需要加密的文本内容
     * @param key 加密的密钥 key
     * @param iv  初始化向量
     */
    public static byte[] encrypt(byte[] key, byte[] iv, byte[] msg) {
        if (isEmpty(msg) || isEmpty(key) || isEmpty(iv)) {
            return null;
        }

        try {
            // 创建AES加密器
            Cipher cipher = Cipher.getInstance(AES_CFB);

            SecretKeySpec secretKeySpec = getSecretKeySpec(key);

            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));

            // 加密字节数组
            byte[] encryptedBytes = cipher.doFinal(msg);

            // 将密文转换为 Base64 编码字符串
//            return Base64.getEncoder().encodeToString(encryptedBytes);
            return encryptedBytes;
        } catch (Exception e) {
            System.out.println("加密异常");
            throw new RuntimeException(e);
        }
    }

    /**
     * <h2>解密 - AES_CFB自定义加密模式</h2>
     *
     * @param msg 需要解密的文本内容
     * @param key 解密的密钥 key
     * @param iv  初始化向量
     */
    public static byte[] decrypt(byte[] key, byte[] iv, byte[] msg) {
        if (isEmpty(msg) || isEmpty(key) || isEmpty(iv)) {
            return null;
        }

        // 将密文转换为16字节的字节数组
//        byte[] textBytes = Base64.getDecoder().decode(msg);

        try {
            // 创建AES加密器
            Cipher cipher = Cipher.getInstance(AES_CFB);

            SecretKeySpec secretKeySpec = getSecretKeySpec(key);

            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(iv));

            // 解密字节数组
            byte[] decryptedBytes = cipher.doFinal(msg);

            // 将明文转换为字符串
//            return new String(decryptedBytes, StandardCharsets.UTF_8);
            return decryptedBytes;
        } catch (Exception e) {
            System.out.println("解密异常");
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
//        String text = "嗨，您好！";
//        String key = "woniucsdnvip8888"; // 16字节的密钥
//        String iv = getIV();
//
//        String encryptTextEBC = encrypt(text, key);
//        System.out.println("EBC 加密后内容：" + encryptTextEBC);
//        System.out.println("EBC 解密后内容：" + decrypt(encryptTextEBC, key));
//        System.out.println();
//
//
//        String encryptTextCBC = encrypt(text, key, iv, AES_CBC);
//        System.out.println("CBC 加密IV：" + iv);
//        System.out.println("CBC 加密后内容：" + encryptTextCBC);
//        System.out.println("CBC 解密后内容：" + decrypt(encryptTextCBC, key, iv, AES_CBC));
//        System.out.println();
//
//        String encryptTextCFB = encrypt(text, key, iv, AES_CFB);
//        System.out.println("CFB 加密IV：" + iv);
//        System.out.println("CFB 加密后内容：" + encryptTextCFB);
//        System.out.println("CFB 解密后内容：" + decrypt(encryptTextCFB, key, iv, AES_CFB));

    }
}
